package rhc.discribute.node.connecter.schedule;

import java.util.concurrent.CountDownLatch;

import org.quartz.Job;

import rhc.discribute.node.connecter.impl.ZKPathLockData;
import rhc.discribute.node.connecter.impl.zookper.ZookperLockImpl;
import rhc.discribute.util.ScheduleUpdate;
import rhc.discribute.util.ScheduleUpdateUtil;

/**一个锁可以设置一个最长等待时间，当时间到后通过定时任务删除掉该锁，
 * @author rhc
 *
 */
public class LockPathSchedule implements ScheduleUpdate{

	/**
	 * 锁数据
	 */
	private ZKPathLockData zkPLD;
	
	/**
	 * 锁信号
	 */
	private CountDownLatch latch;
	
	/**
	 * 
	 */
	private ZookperLockImpl zkLock;
	
	/**
	 * 等待毫秒数
	 */
	private long waitSeconds;
	
	public LockPathSchedule(ZookperLockImpl zkLock,ZKPathLockData zkPLD,CountDownLatch latch,long waitSeconds){
		this.zkLock=zkLock;
		this.zkPLD=zkPLD;
		this.latch=latch;
		this.waitSeconds=waitSeconds;
	}
	
	public void run(){
		ScheduleUpdateUtil.runJob(this);
	}
	
	@Override
	public String getId() {
		return zkPLD.getEphePath();
	}

	@Override
	public ScheduleType getScheduleType() {
		return ScheduleType.TIMEINTERVAL;
	}

	@Override
	public int getTimeInterval() {
		return new Long(waitSeconds).intValue();
	}

	@Override
	public boolean isForever() {
		return false;
	}

	@Override
	public int runCount() {
		return 1;
	}

	@Override
	public boolean startNow() {
		return false;
	}

	@Override
	public String getCron() {
		return null;
	}

	public ZKPathLockData getZkPLD() {
		return zkPLD;
	}

	public void setZkPLD(ZKPathLockData zkPLD) {
		this.zkPLD = zkPLD;
	}

	public CountDownLatch getLatch() {
		return latch;
	}

	public void setLatch(CountDownLatch latch) {
		this.latch = latch;
	}

	public long getWaitSeconds() {
		return waitSeconds;
	}

	public void setWaitSeconds(long waitSeconds) {
		this.waitSeconds = waitSeconds;
	}

	public ZookperLockImpl getZkLock() {
		return zkLock;
	}

	public void setZkLock(ZookperLockImpl zkLock) {
		this.zkLock = zkLock;
	}
	
	public void clear(){
		this.zkLock=null;
		this.zkPLD=null;
		this.latch=null;
	}

	@Override
	public Class<? extends Job> getJobClass() {
		return LockPathJob.class;
	}

}
